package com.rs.controller;

import java.awt.BorderLayout;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.border.EmptyBorder;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.rs.model.CommonResult;
import com.rs.model.ZhDepartment;
import com.rs.model.ZhPosition;
import com.rs.model.ZhUser;
import com.rs.service.PublicUserMethodsService;
import com.rs.service.ZhClassesService;
import com.rs.service.ZhDepartmentService;
import com.rs.service.ZhPositionService;
import com.rs.service.ZhUserStudentService;
import com.rs.service.ZhUserTeacherService;
import com.rs.util.DateChooserJButton;
import com.rs.util.DateFormatUtil;
import com.rs.util.SpringContextUtils;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SpinnerDateModel;

import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.awt.event.ActionEvent;
import java.awt.Scrollbar;
import java.awt.Checkbox;
import javax.swing.JSeparator;
import javax.swing.JComboBox;
import javax.swing.SwingConstants;
import java.awt.Toolkit;
import javax.swing.ImageIcon;


@SuppressWarnings("all")
public class AddTeacher extends JInternalFrame {
	

	private PublicUserMethodsService pum = SpringContextUtils.getBean(PublicUserMethodsService.class);
	
	
	private ZhUserTeacherService zhUserTeacherService = SpringContextUtils.getBean(ZhUserTeacherService.class) ;
	
	int  sum=0;  //为0表示 为添加 为1表示为查询
	int  id; 

	private JPanel contentPane;
	private JTextField text_username;
	private JTextField text_pass;
	private JTextField text_name;
	private JTextField text_phone;
	private JTextField text_card;
	private JTextField text_sex;
	private JTextField text_age;
	private JTextField text_addess;
	private JTextField text_img;
	private JTextField text_de;
	private JTextField text_op;
	private JTextField text_day;
	private JPanel panel_1;
	private JComboBox comboBox_de;
	private JComboBox comboBox_po;
	private JButton btnNewButton;
	
	Map<String, Integer> mapde = new HashMap<>();
	Map<String, Integer> mappo = new HashMap<>();
	
	
	
	DateChooserJButton dc= new DateChooserJButton();
	
	JLabel lblUusernameUpasswordUnameuphone;
	
	SpinnerDateModel model = new SpinnerDateModel();
     //���JSPinner����
     JSpinner year = new JSpinner(model);

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AddTeacher frame = new AddTeacher();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public void aa(){
//		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 700, 600);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		if(sum==0){
		lblUusernameUpasswordUnameuphone = new JLabel("添加教师");
		lblUusernameUpasswordUnameuphone.setHorizontalAlignment(SwingConstants.CENTER);
		lblUusernameUpasswordUnameuphone.setFont(new Font("����", Font.PLAIN, 18));
		lblUusernameUpasswordUnameuphone.setBounds(234, 24, 131, 27);
		contentPane.add(lblUusernameUpasswordUnameuphone);
		}else{
			lblUusernameUpasswordUnameuphone = new JLabel("教师信息");
			lblUusernameUpasswordUnameuphone.setFont(new Font("����", Font.PLAIN, 18));
			lblUusernameUpasswordUnameuphone.setBounds(276, 25, 131, 27);
			contentPane.add(lblUusernameUpasswordUnameuphone);
		}
		
		JLabel label = new JLabel("\u8D26\u53F7\uFF1A");
		label.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		label.setBounds(45, 82, 54, 15);
		contentPane.add(label);
		
		JLabel lblNewLabel = new JLabel("\u5BC6\u7801\uFF1A");
		lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		lblNewLabel.setBounds(45, 136, 54, 15);
		contentPane.add(lblNewLabel);
		
		JLabel label_1 = new JLabel("\u59D3\u540D\uFF1A");
		label_1.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		label_1.setBounds(45, 188, 54, 15);
		contentPane.add(label_1);
		
		JLabel label_2 = new JLabel("\u624B\u673A\u53F7\uFF1A");
		label_2.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		label_2.setBounds(45, 247, 54, 15);
		contentPane.add(label_2);
		
		JLabel label_3 = new JLabel("\u8EAB\u4EFD\u8BC1\u53F7\uFF1A");
		label_3.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		label_3.setBounds(45, 301, 67, 15);
		contentPane.add(label_3);
		
		JLabel label_4 = new JLabel("\u6027\u522B\uFF1A");
		label_4.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		label_4.setBounds(45, 354, 54, 15);
		contentPane.add(label_4);
		
		JLabel label_5 = new JLabel("\u5E74\u9F84;");
		label_5.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		label_5.setBounds(45, 412, 54, 15);
		contentPane.add(label_5);
		
		text_username = new JTextField();
		text_username.setBounds(114, 79, 148, 27);
		contentPane.add(text_username);
		text_username.setColumns(10);
		
		text_pass = new JTextField();
		text_pass.setColumns(10);
		text_pass.setBounds(114, 130, 148, 27);
		
		contentPane.add(text_pass);
		
		text_name = new JTextField();
		text_name.setColumns(10);
		text_name.setBounds(114, 182, 148, 27);
		contentPane.add(text_name);
		
		text_phone = new JTextField();
		text_phone.setColumns(10);
		text_phone.setBounds(114, 244, 148, 27);
		contentPane.add(text_phone);
		
		text_card = new JTextField();
		text_card.setColumns(10);
		text_card.setBounds(114, 295, 148, 27);
		contentPane.add(text_card);
		
		text_sex = new JTextField();
		text_sex.setColumns(10);
		text_sex.setBounds(114, 348, 148, 27);
		contentPane.add(text_sex);
		
		text_age = new JTextField();
		text_age.setColumns(10);
		text_age.setBounds(114, 400, 148, 27);
		contentPane.add(text_age);
		
		JLabel label_6 = new JLabel("\u4F4F\u5740\uFF1A");
		label_6.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		label_6.setBounds(338, 82, 54, 15);
		contentPane.add(label_6);
		
		JLabel label_7 = new JLabel("\u5934\u50CF\uFF1A");
		label_7.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		label_7.setBounds(338, 136, 54, 15);
		contentPane.add(label_7);
		
		JLabel label_8 = new JLabel("\u90E8\u95E8\uFF1A");
		label_8.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		label_8.setBounds(338, 188, 54, 15);
		contentPane.add(label_8);
		
		JLabel label_9 = new JLabel("\u804C\u52A1\uFF1A");
		label_9.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		label_9.setBounds(338, 247, 54, 15);
		contentPane.add(label_9);
		
		JLabel label_10 = new JLabel("\u51FA\u751F\u65E5\u671F\uFF1A");
		label_10.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		label_10.setBounds(338, 301, 69, 15);
		contentPane.add(label_10);
		
 		
 		if(sum==0){
 			 panel_1 = new JPanel();
 	 		  panel_1.setBounds(402, 298, 148, 27);
 	 		 contentPane.add(panel_1);
 	 		   
 	 		 year.setValue(new Date());
 	         //����ʱ���ʽ
 	         JSpinner.DateEditor editor = new JSpinner.DateEditor(year,"yyyy-MM-dd");
 	         year.setEditor(editor);
 	         year.setBounds(402, 298, 148, 27);
// 	 		 panel_1 ����Ҫ��ӽ���  ������
 	       
 	 		  panel_1.add(dc);
 	 		 
 	 		  
 	 		  
 				

 				comboBox_de = new JComboBox();
 				comboBox_de.setFont(new Font("等线", Font.PLAIN, 18));
 				comboBox_de.setBounds(404, 182, 146, 27);
 				contentPane.add(comboBox_de);
 				List<ZhDepartment> re =  pum.selectDeAll();
				for(ZhDepartment de:re){
				comboBox_de.addItem(de.getDeName());
				mapde.put(de.getDeName(),de.getDeId());
				}
 				
 					
 				comboBox_po = new JComboBox();
 				comboBox_po.setFont(new Font("等线", Font.PLAIN, 18));
 				comboBox_po.setBounds(404, 241, 146, 27);
 				contentPane.add(comboBox_po);
 				List<ZhPosition> rep = pum.selectPoAll();
				for(ZhPosition po:rep){
				comboBox_po.addItem(po.getPoName());
				mappo.put(po.getPoName(),po.getPoId());
				}
 				
 				
 				
 				btnNewButton = new JButton("\u63D0\u4EA4");
 				btnNewButton.addActionListener(new ActionListener() {
 					public void actionPerformed(ActionEvent e) {
 						int b = pum.nameRepetition(text_username.getText());
 						if(b>0){
 							alert("昵称一被用!");
 						}
 						
 						notNull();
 						ZhUser user=new ZhUser();
 						user.setuUsername(text_username.getText());
 						user.setuPassword(text_pass.getText());
 						user.setuName(text_name.getText());
 						user.setuPhone(text_phone.getText());
 						user.setuIdcard(text_card.getText());
 						user.setuSex(text_sex.getText());
 						user.setuAge(Integer.parseInt(text_age.getText()));
 						//user.setuBirthday(text_day.getText());
 						user.setuBirthday(dc.getDate());
 						user.setuAddress(text_addess.getText());
 						user.setuHead(text_img.getText());
 						//user.setuDeid(Integer.parseInt(text_de.getText()));
 						//user.setuPoid(Integer.parseInt(text_op.getText()));
 						user.setuDeid(mapde.get(comboBox_de.getSelectedItem()));
 						user.setuPoid(mappo.get(comboBox_po.getSelectedItem()));
 						
 						CommonResult<Integer> co =zhUserTeacherService.addTeacher(user);
 						if(co.getData()>0){
 							alert("添加成功！");
 						}else{
 							alert("添加失败！");
 						}
 					}
 				});
 				btnNewButton.setBounds(402, 408, 131, 41);
 				contentPane.add(btnNewButton);
 	 		  
 	 		  
 		}else{
 			text_day = new JTextField()	;	
 	 		text_day.setColumns(10);
 	 		text_day.setBounds(402, 298, 148, 27);
 	 		contentPane.add(text_day);
 		}
		
		text_addess = new JTextField();
		text_addess.setColumns(10);
		text_addess.setBounds(402, 79, 148, 27);
		contentPane.add(text_addess);
		
		text_img = new JTextField();
		text_img.setColumns(10);
		text_img.setBounds(402, 133, 148, 27);
		contentPane.add(text_img);
		

		
		if(sum==1){
			text_de = new JTextField();
			text_de.setColumns(10);
			text_de.setBounds(402, 185, 148, 27);
			contentPane.add(text_de);
			
			text_op = new JTextField();
			text_op.setColumns(10);
			text_op.setBounds(402, 244, 148, 27);
			contentPane.add(text_op);
			
			JButton Teacher_del = new JButton("点击删除");
			Teacher_del.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int a = pum.userDel(id);
					alert(a>0?"删除成功":"删除失败");
					close();
				}
			});
			Teacher_del.setBounds(338, 399, 95, 40);
			contentPane.add(Teacher_del);
			
			JButton Teacher_upd = new JButton("点击修改");
			Teacher_upd.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					sum=0;
					aa();
					ZhUser zh=pum.selectUserId(id);
					if(zh ==null){
						alert("查无此人！！");
						return;
					}
					try {
							text_day = new JTextField()	;	
				 	 		text_day.setColumns(10);
				 	 		text_day.setBounds(402, 298, 148, 27);
				 	 		
							text_username.setText(zh.getuUsername());
							text_pass.setText("*");
							text_name.setText(zh.getuName());
							text_phone.setText(zh.getuPhone());
							text_card.setText(zh.getuIdcard());
							text_sex.setText(zh.getuSex());
							text_age.setText(zh.getuAge()+"");
							text_day.setText(DateFormatUtil.getDateStringw(zh.getuBirthday(),"yyyy-MM-dd"));
							text_addess.setText(zh.getuAddress());
							text_img.setText(zh.getuHead());
							comboBox_de.setSelectedItem(pum.selectDe(zh.getuDeid()));
							comboBox_po.setSelectedItem(pum.selectPo(zh.getuPoid()));
						
						contentPane.remove(panel_1);
						
			 	 		contentPane.add(text_day);
			 	 		lblUusernameUpasswordUnameuphone.setText("修改信息");
			 	 		btnNewButton.addActionListener(new ActionListener() {
		 					public void actionPerformed(ActionEvent e) {
		 						ZhUser user=new ZhUser();
		 						user.setuId(id);
		 						user.setuUsername(text_username.getText());
		 						user.setuName(text_name.getText());
		 						user.setuPhone(text_phone.getText());
		 						user.setuIdcard(text_card.getText());
		 						user.setuSex(text_sex.getText());
		 						user.setuAge(Integer.parseInt(text_age.getText()));
		 						user.setuAddress(text_addess.getText());
		 						user.setuHead(text_img.getText());
		 						user.setuDeid(mapde.get(comboBox_de.getSelectedItem()));
		 						user.setuPoid(mappo.get(comboBox_po.getSelectedItem()));
		 						
		 						CommonResult<Integer> co= zhUserTeacherService.updateTeacher(user);
		 						alert(co.getData()>0?"修改成功":"修改失败");
		 						
		 					}
			 	 		});
				
						text_pass.setEditable(false);
						text_day.setEditable(false);
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			});
			Teacher_upd.setBounds(459, 399, 95, 40);
			contentPane.add(Teacher_upd);
		}
			
			
		if(id != 0 && sum==1){
			selectTeacherId();
		}
		
	}
	
	public void notNull(){
		if(text_username.getText() ==null || text_username.getText().length()==0){
			alert("账号不能为空!");
			return;
		}
		if(text_pass.getText() ==null || text_pass.getText().length()==0){
			alert("密码不能为空!");
			return;
		}
		if(text_name.getText() ==null || text_name.getText().length()==0){
			alert("姓名不能为空!");
			return;
		}
		if(text_phone.getText() ==null || text_phone.getText().length()==0){
			alert("手机号不能为空!");
			return;
		}
		
		if(text_card.getText() ==null || text_card.getText().length()==0){
			alert("身份证号不能为空!");
			return;
		}
		if(text_sex.getText() ==null || text_sex.getText().length()==0){
			alert("性别不能为空！");
			return;
		}
		if(text_age.getText() ==null || text_age.getText().length()==0){
			alert("年龄不能为空!");
			return;
		}
	/*	if(panel_1.get()==null || text_day.getText().length() ==0 ){
			alert("�������ڲ���Ϊ��!");
			return;
		}*/
		if(text_addess.getText() ==null || text_addess.getText().length()==0){
			alert("地址不能为空!");
			return;
		}
		if(text_img.getText() ==null || text_img.getText().length()==0){
			alert("头像不能为空!");
			return;
		}
		
	/*	if(text_de.getText() ==null || text_de.getText().length()==0){
			alert("部门不能为空!");
			return;
		}
		if(text_op.getText() ==null || text_op.getText().length()==0){
			alert("职务不能为空!");
			return;
		}*/
	}
	
	
	
	
	
	public void selectTeacherId(){
		
		ZhUser zh =pum.selectUserId(id);
		
		text_username.setText(zh.getuUsername());
		text_pass.setText("*");
		text_name.setText(zh.getuName());
		text_phone.setText(zh.getuPhone());
		text_card.setText(zh.getuIdcard());
		text_sex.setText(zh.getuSex());
		text_age.setText(zh.getuAge()+"");
		text_day.setText(DateFormatUtil.getDateStringw(zh.getuBirthday(),"yyyy-HH-mm"));
		text_addess.setText(zh.getuAddress());
		text_img.setText(zh.getuHead());
		comboBox_de.setSelectedItem(pum.selectDe(zh.getuDeid()));
		comboBox_po.setSelectedItem(pum.selectPo(zh.getuPoid()));
			
		
		text_username.setEditable(false);
		text_pass.setEditable(false);
		text_name.setEditable(false);
		text_phone.setEditable(false);
		text_card.setEditable(false);
		text_sex.setEditable(false);
		text_age.setEditable(false);
		text_de.setEditable(false);
		text_op.setEditable(false);
		text_day.setEditable(false);
		text_addess.setEditable(false);
		text_img.setEditable(false);
	}
	public void alert(String alert){
		JOptionPane.showMessageDialog(this, alert);
	}

	/**
	 * Create the frame.
	 */
	public AddTeacher() {
		setIconifiable(true);
		setMaximizable(true);
		setClosable(true);
		setTitle("添加教师");
		aa();
	}
	public AddTeacher(int sum,int id) {
		this.sum=sum;
		this.id=id;
		aa();
	} 
	void close(){
		this.dispose();
	}
}
